package com.erhuogo.action;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.Response;
import com.erhuogo.entity.Admin;
import com.erhuogo.enumerate.Result;
import com.erhuogo.service.AdminService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 管理员Action
 * @author aibinxiao
 * @date 2016年10月26日 上午11:27:27
 */
public class AdminAction extends ActionSupport{
	private static final long serialVersionUID = 6194097043505950121L;
	private static Logger logger = LoggerFactory.getLogger(AdminAction.class);
	
	// 模型驱动使用的对象
	private Admin admin;
	
	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	// 注入AdminService
	private AdminService adminService;
	
	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	
	// 返回页面数据
	private Response response = new Response();

	public Response getResponse() {
		return response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}
	
	/**
	 * 登录的方法
	 */
	public String login() {
		Admin existAdmin = adminService.login(admin.getLoginName(), admin.getPassword());
		// 判断
		if (existAdmin == null) {
			response.setResult(Result.FAILED.name());
			response.setMessage("登陆失败");
		} else {
			response.setResult(Result.SUCCESS.name());
			response.setMessage(existAdmin.getLoginName());
			ServletActionContext.getRequest().getSession().setAttribute("existAdmin", existAdmin);
		}
		return "loginSuccess";
	}

}
